package com.concurrent.atomic;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author: zhouyang
 * @descrpition:
 * @time: 2019-05-04 17:10
 */
@Slf4j
public class AtomicFieldUpdater {
    private static AtomicIntegerFieldUpdater<AtomicFieldUpdater>  updater =
        AtomicIntegerFieldUpdater.newUpdater(AtomicFieldUpdater.class,"count");

    @Getter
    private volatile int count = 100;//必须要用这个关键字来修饰

    private static AtomicFieldUpdater afu = new AtomicFieldUpdater();

    public static void main(String[] args) {
        if(updater.compareAndSet(afu,100,120)){
            log.info("update success1, {}",afu.getCount());
        }

        if(updater.compareAndSet(afu,100,120)){
            log.info("update success2, {}",afu.getCount());
        }else{
            log.info("update fail,{}",afu.getCount());
        }
    }
}
